package com.dmm.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.dmm.redis.service.redisService;
import com.dmm.user.entity.Emp;
import com.dmm.user.mapper.EmpMapper;
import com.dmm.user.service.EmpService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dmm
 * @since 2022-10-14
 */
@Service
@Slf4j
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {

    @Resource
    redisService redisService;
//    @Resource
//    RedisTemplate<String,String> redisTemplate;

    @Override
    public List<Emp> Findall() {

        //String emp= (String)redisTemplate.opsForValue().get("EmpService:FindAll");

        //获取
        List<Emp> emp = redisService.get("EmpService:FindAll", Emp.class);

        if(emp!=null){


            log.info("走的缓存");
            return emp;

        }else{

            List<Emp> emps = baseMapper.selectList(new QueryWrapper<>());
            if(emps!=null || emps.size()>0){
                /**
                 * TimeUnit.DAYS   每天
                 * TimeUnit.HOURS  小时
                 * TimeUnit.MINUTES   分钟
                 * TimeUnit.SECONDS   秒
                 * TimeUnit.MILLISECONDS  毫秒
                 * TimeUnit.MICROSECONDS  微秒
                 * TimeUnit.NANOSECONDS  纳秒
                 */
                log.info("进入前");
               // redisTemplate.opsForValue().set("EmpService:FindAll",JSON.toJSONString(emps),60, TimeUnit.MINUTES);

                redisService.set("EmpService:FindAll",emps,60);

                log.info("进入后");
            }else{
                //解决缓存击穿
                //redisTemplate.opsForValue().set("EmpService:FindAll",null,60, TimeUnit.MINUTES);

                redisService.set("EmpService:FindAll",null,60);
            }
            return emps;

        }

    }


    @Override
    public void savabyid(Emp emp) {


        Boolean delete = redisService.delete("EmpService:FindAll");


        QueryWrapper<Emp> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",emp.getEmpNumber());
        baseMapper.update(emp,queryWrapper);

    }
}
